#include <iostream>
#include <string.h>
using std::cout;
using std::endl;

class Person{
public:
    Person(const char* name,int age)
    :_name(new char[strlen(name)+1]())
    ,_age(age)
    {
        strcpy(_name,name);
    }

    Person(const Person & rhs)
    :_name(new char[strlen(rhs._name)+1]())
    ,_age(rhs._age)
    {
        strcpy(_name,rhs._name);
    }

    Person & operator=(const Person & rhs)
    {
        if(this != &rhs){
            delete [] _name;
            _name = new char[strlen(rhs._name)+1]();
            strcpy(_name,rhs._name);
            _age = rhs._age;
        }
        return *this;
    }

    ~Person(){
        if(_name){
            delete [] _name;
            _name = nullptr;
        }
    }

    void display(){
        cout << "name:" << _name
             << "age:" << _age << endl;
    }

protected:
    char* _name;
    int _age;
};

class Employee
:public Person
{
public:
    Employee(const char* name,int age,const char* department,int salary)
    :Person(name,age)
    ,_department(new char[strlen(department)+1]())
    ,_salary(salary)
    {
        strcpy(_department,department);
    }

    Employee(const Employee & rhs)
    :Person(rhs)
    ,_department(new char[strlen(rhs._department)+1]())
    ,_salary(rhs._salary)
    {
        strcpy(_department,rhs._department);
    }

    Employee & operator=(const Employee & rhs){
        if(this != &rhs){
            Person::operator=(rhs);
            delete [] _department;
            _department = new char[strlen(rhs._department)+1]();
            strcpy(_department,rhs._department);
            _salary = rhs._salary;
        }
        return *this;
    }

    ~Employee(){
        if(_department){
            delete _department;
            _department =nullptr;
        }
    }

    void display(){
        cout << _name << " "
             << _age  << " "
             << _department << " "
             << _salary << endl;
    }

private:
    char* _department;
    int _salary;

};

void test0(){
    Employee emp("xiaoming",21,"yanfa",20000);
    emp.display();

    Employee emp1("wang",24,"xixi",8000);
    emp = emp1;
    emp.display();

    Employee emp2 = emp;
    emp2.display();
}

int main(int argc, char * argv[])
{
    test0();
    return 0;
}


